package edu.sjsu.itack.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.sjsu.itack.dao.TackDao;
import edu.sjsu.itack.dao.BoardDao;
import edu.sjsu.itack.entity.Board;
import edu.sjsu.itack.entity.Tack;
import edu.sjsu.itack.entity.User;
import edu.sjsu.itack.util.ITackException;

/**
 * Servlet implementation class ViewTack
 */
@WebServlet("/ViewTack")
public class ViewTack extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewTack() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("In ViewServlet: doGet method");
		TackDao tack=new TackDao();
		User user=(User)request.getSession().getAttribute("user");
		System.out.println(user.getUserId());
		try {
			System.out.println("In ViewServlet: doget: try");
			Tack[] tackArr=tack.getAllTacksForUser((int)user.getUserId());
			request.getSession().setAttribute("tackArr", tackArr);
			System.out.println("In viewTack.java servlet: tack Arr length= "+tackArr.length);
			request.getRequestDispatcher("ViewTack.jsp").forward(request, response);
		} catch (ITackException e) {
			System.out.println("In ViewServlet: doget: exception");
			// TODO Auto-generated catch block
			//status.append(e.getLocalizedMessage());
			request.setAttribute("error", e.getLocalizedMessage());
			request.getRequestDispatcher("ViewTack.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
